package com.wms.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.User;
import com.wms.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器erg sd
 * </p>
 *
 * @author liangzai
 * @since 2023-11-09
 */
@RestController
@RequestMapping("/user")
@Api(value = "UserController", tags = "用户管理")

public class UserController {
    @Autowired
    private UserService userService;


    @ApiOperation("列表")
    @GetMapping("/list")
    public Result list() {

        return Result.suc(userService.list());
    }


    @ApiOperation("分页列表")
    @GetMapping("/page")
    public Result page(@RequestParam int page,
                       @RequestParam int size,
                       @RequestParam(required = false) String name,
                       @RequestParam(required = false) String sex) {
        Page<User> userPage = new Page<>(page, size);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(User::getName, name);
        }
        if (StrUtil.isNotBlank(sex)) {
            queryWrapper.like(User::getSex, sex);
        }
        return Result.suc(userService.page(userPage, queryWrapper));

    }


    @ApiOperation("分页列表")
    @PostMapping("/listPageC1")
    public Result page(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String sex = (String) param.get("sex");
        String roleId = (String) param.get("roleId");

        Page<User> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(User::getName, name);
        }
        if (StringUtils.isNotBlank(sex)) {
            lambdaQueryWrapper.eq(User::getSex, sex);
        }
        if (StringUtils.isNotBlank(roleId)) {
            lambdaQueryWrapper.eq(User::getRoleId, roleId);
        }

        //IPage result = userService.pageC(page);
        Page result = userService.page(page, lambdaQueryWrapper);

        System.out.println("total==" + result.getTotal());

        return Result.suc(result.getRecords(), result.getTotal());
    }

    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String no) {
        List<User> list = userService.lambdaQuery().eq(User::getNo, no).list();
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    @ApiOperation("分页列表")
    @GetMapping("/pagetest")
    public Result pagetest(@RequestParam int page,
                           @RequestParam int size,
                           @RequestParam(required = false) String name,
                           @RequestParam(required = false) Integer age,
                           @RequestParam(required = false) Integer sex) {
        Page<User> userPage = new Page<>(page, size);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(User::getName, name);
        }
        if (age != null) {
            queryWrapper.eq(User::getAge, age);
        }
        if (sex != null) {
            queryWrapper.eq(User::getSex, sex);
        }
        return Result.suc(userService.page(userPage, queryWrapper));

    }


    @ApiOperation("新增")
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.suc() : Result.fail();
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.suc() : Result.fail();
    }

    @ApiOperation("新增或修改")
    @PostMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }


    @ApiOperation("删除")
    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {
        return userService.removeById(id) ? Result.suc() : Result.fail();
    }
}
